//
//  YZXKeyboardCell.swift
//  表情键盘
//
//  Created by 叶周翔 on 16/7/29.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit
// 一页表情有20个按钮
let emojiPage = 20
// 一共有7列
let emojiCols = 7
// 一共有三行
let emojiRow = 3

protocol YZXKeyboardCellDelegate:NSObjectProtocol{
    // 定义方法
    // 表情按钮点击的方法
    func emojiBtnClick(emojiModel model:YZXEmoticonModel)
    // 删除按钮点击的方法
    func deleteBtnDidClick() ->Void
}

class YZXKeyboardCell: UICollectionViewCell {
    // 定义代理属性
    weak var delegate: YZXKeyboardCellDelegate?
    
    private var emojiButtons:[YZXEmoticonButton] = [YZXEmoticonButton]()
    
    var indexPath: NSIndexPath?{
        didSet{
            debugLabel.text = "现在是第\(indexPath!.section)组 第\(indexPath!.item)行"
            // 获取到indexPath的时候控制recentLabel的显示
            recentLabel.hidden = (indexPath?.section != 0)
        }
    }
    /// 一个cell要显示20个模型
    var pageEmoticons: [YZXEmoticonModel]? {
        didSet {
            for emoticonButton in emojiButtons{
                emoticonButton.hidden = true
            }
            
            for (index,emotconModel) in pageEmoticons!.enumerate(){
                let emoticonBtn = emojiButtons[index]
                // 将模型赋值给按钮
                emoticonBtn.emojiconModel = emotconModel
                emoticonBtn.hidden = false
            }
        }
    }
    // MARK:view的生命周期
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
    }
    
    
    private func setUI() {
        // 创建表情
        creatEmoticons()
        
//        self.contentView.addSubview(debugLabel)
        self.contentView.addSubview(deleteBtn)
        self.contentView.addSubview(recentLabel)
        // 设置约束
        recentLabel.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraint(NSLayoutConstraint(item: recentLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: recentLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -5))
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layouEmojiButtons()
    }
    
    // MARK:-
    // 创建20个表情按钮
    private func creatEmoticons(){
        for _ in 0..<emojiPage{
            let button = YZXEmoticonButton()
//            button.backgroundColor = UIColor.randomColor()
            self.contentView.addSubview(button)
            emojiButtons.append(button)
        }
        
    }
    
    private func layouEmojiButtons(){
        let leftMatgin:CGFloat = 5
        let bottomMargin:CGFloat = 25
        // 计算一个按钮的行高
        let itemWidth = (self.frame.size.width - 2 * leftMatgin) / CGFloat(emojiCols)
        let itemHeight = (self.frame.size.height - bottomMargin) / CGFloat(emojiRow)
        
        for (index,button) in emojiButtons.enumerate() {
            
            // 计算列数
            let cols = index % emojiCols
            let rows = index / emojiCols
            let x = leftMatgin + CGFloat(cols) * itemWidth
            let y = CGFloat(rows) * itemHeight
            button.frame = CGRect(x: x, y: y, width: itemWidth, height: itemHeight)
            // 添加点击事件
            button.addTarget(self, action: "emojiBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        }
        // 删除按钮的位置 是在第2行的第6列
        let x = leftMatgin + CGFloat(emojiCols - 1) * itemWidth
        let y = CGFloat(emojiRow - 1) * itemHeight
        deleteBtn.frame = CGRect(x: x, y: y, width: itemWidth, height: itemHeight)
        
    }
    // MARK:- 按钮点击事件
    @objc private func emojiBtnDidClick(btn:YZXEmoticonButton) {
        // 调用代理方法 将模型传过去
        
        
        delegate?.emojiBtnClick(emojiModel: btn.emojiconModel!)
        
    }
    
    @objc private func deleteContent() {
        
        delegate?.deleteBtnDidClick()
        
    }
    

    // MARK: - 懒加载
    // 调试label
    private lazy var debugLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 370, height: 200))
        
        // 设置
        label.textColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(25)
        
        return label
    }()
    
    
    private lazy var deleteBtn:UIButton = {
        let button = UIButton()
        
        // 设置
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        // 添加点击事件
        button.addTarget(self, action: "deleteContent", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    private lazy var recentLabel:UILabel = {
       let label = UILabel()
        label.text = "最近使用的表情"
        label.sizeToFit()
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        return label
    }()
    
    
    
}